//102. 二叉树的层序遍历
// 思路：层节点个数配合队列


#include <queue>
using namespace std;

struct TreeNode 
{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
class Solution 
{
public:
    vector<vector<int>> levelOrder(TreeNode* root) 
    {
        queue<TreeNode*> q;
        int LevelSize = 0;
        if(root)
        {
            q.push(root);
            LevelSize = 1;
        }

        vector<vector<int>> vv;
        while(!q.empty())
        {
            // 一层出完
            vector<int> v;
            while(LevelSize--)
            {
                TreeNode* front = q.front();
                q.pop();
                v.push_back(front->val);

                if(front->left)
                    q.push(front->left);
                if(front->right)
                    q.push(front->right);
            }
            vv.push_back(v);
            // 队列中数据个数就是下一层的数据个数
            LevelSize = q.size();
        }
        return vv;
    }
};